package com.kehao.leetcode.jzoffer;

import java.util.Stack;

/**
 * @author Kehao.Chen
 * @version 1.0
 * @date 2022/6/28 17:15
 */
public class JZSolution036 {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (String token : tokens) {
            if(token.equals("+")){
                Integer num2 = stack.pop();
                Integer num1 = stack.pop();
                stack.push(num1+num2);
            }else if(token.equals("-")){
                Integer num2 = stack.pop();
                Integer num1 = stack.pop();
                stack.push(num1-num2);
            }else if(token.equals("*")){
                Integer num2 = stack.pop();
                Integer num1 = stack.pop();
                stack.push(num1*num2);
            }else if(token.equals("/")){
                Integer num2 = stack.pop();
                Integer num1 = stack.pop();
                stack.push(num1/num2);
            }else {
                stack.add(Integer.parseInt(token));
            }
        }
        return stack.pop();
    }
}
